<?php

$action = $_GET['action'];
$m = $_GET['m'];

$album_id = intval($_GET['album_id']);

include_once('model/photo_album.php');
$album = new LZ_Photo_Album;
include_once('model/photo.php');
$photo = new LZ_Photo;

cache_this_page();
if (!$album_id)
{
	$total = $album->get_list( array('select' => 'count(album_id)','status' => 'public'));
	$total = $total[0]['count(album_id)'];
	$page = intval($_GET['page']);
	$per_page = (intval($config['albums_per_page_front']))?intval($config['albums_per_page_front']):20;
	!$page && $page=1;
	$total_page = ceil($total/$per_page);
	!$total_page && $total_page=1;
	$page>$total_page && $page = $total_page;
	$albums_arr = $album->get_list( array('from' => ($page-1)*$per_page,'total'=>$per_page,'status' => 'public'));

	$pager = PAGER_TOTAL.$total.PAGER_ITEMS.' ';
	$total_page > 1 && $pager.= $total_page.PAGER_PAGE;
	$pager.= '<br />';
	$pager.= html_pager("?p=photo_album",$total_page,$page);
	$view_data['pager'] = $pager;
	
	$albums = array();
	foreach($albums_arr as $album_arr)
	{
		if ($album_arr['cover_id']>0)
			$_cover_id = $album_arr['cover_id'];
		else
		{
			$_ids = $album_arr['photo_ids'];
			if ($_ids)
			{
				$_arr = explode(',',$_ids);
				if (intval($_arr[0]))
				{
					$_cover_id = intval($_arr[0]);
				}
			}
		}
		if ($_cover_id>0)
		{
			$album_arr['cover'] = $photo->get_one($_cover_id);
			if ($album_arr['cover']['photo_id']) $albums[] = $album_arr;
		}
	}
	$view_data['page_description'] = ALBUMS_LIST;
	$view_data['title'] = TITLE;
	$view_data['site_description'] = ALBUMS_LIST;
	$view_data['site_keywords'] = PHOTO_ALBUM;
}
else
{
	$view_data['action'] = 'view_images';
	$view_data['this_album'] = $album->get_one($album_id);
	$arr = explode(',',$view_data['this_album']['photo_ids']);
	
	$total = count($arr);
	$page = intval($_GET['page']);
	$per_page = (intval($config['images_per_album_front']))?intval($config['images_per_album_front']):30;
	!$page && $page=1;
	$total_page = ceil($total/$per_page);
	!$total_page && $total_page=1;
	$page>$total_page && $page = $total_page;

	$key_from = ($page-1)*$per_page;
	$key_to = $page*$per_page;
	
	
	$images = array();
	include_once('model/photo.php');
	$photo = new LZ_Photo;
	for($i=$key_from;$i<$key_to;$i++)
	{
		$id = intval($arr[$i]);
		if (!$id) continue;
		$img = $photo->get_one($id);
		if ($img) $images[] = $img;
	}
	$view_data['album_images'] = $images;
	$view_data['album_id'] = $album_id;
	$view_data['page_description'] = $view_data['this_album']['name'];
	$view_data['site_description'] = PHOTO_ALBUM.':'.$view_data['this_album']['name'].' '.$view_data['this_album']['description'];
	$view_data['site_keywords'] = PHOTO_ALBUM.','.$view_data['this_album']['name'];
	$view_data['title'] = $view_data['this_album']['name'].' - '.PHOTO_ALBUM;
	$pager = PAGER_TOTAL.$total.PAGER_ITEMS.' ';
	$total_page > 1 && $pager.= $total_page.PAGER_PAGE;
	$pager.= '<br />';
	$pager.= html_pager("?p=photo_album&album_id=$album_id",$total_page,$page);
	$view_data['pager'] = $pager;
}








$_SESSION['last_list_page'] = "admin.php?p=photo_album&album_id=$album_id&action=sort";

$view_data['albums'] = $albums;
$view_data['err_msg'] = $err_msg;
$view_data['success_msg'] = $success_msg;
$view_data['album_id'] = $album_id;
include_once('left.php');

header('content-type:text/html; charset=utf-8');

